<template>
	<view>
		<view class="feedback-title">
			意见反馈：
		</view>
		<view class="feedback-content">
			<textarea v-model="content" placeholder="请输入您要反馈的问题" class="feedback-text"/>
		</view>
		<view class="feedback-title">
			反馈图片：
		</view>
		<view class="feedback-image-box">
			<view v-for="(item, i) in imageList" :key="i" class="info-stre-image-item">
				<view class="close-icon" @click="del(i)">
					<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
				</view>
				<view class="image-box">
					<image :src="item.url" mode=""></image>
				</view>
			</view>
			<view v-if="imageList.length<6" class="info-stre-image-item" @click="addImage">
				<view class="image-box">
					<uni-icons type="plusempty" size="50" color="#ddd"></uni-icons>
				</view>
			</view>
		</view>
			<button @click="submit" class="feedback-btn" type="primary">提交反馈</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imageList:[],
				content:'',
				timeID:null,
			};
		},
		methods:{
			del(i){
				this.imageList.splice(i,1)
			},
			async submit(){
				uni.showLoading()
				let imagePath=[]
				for(let i=0;i<this.imageList.length; i++){
					const filePath = this.imageList[i].url
					console.log(filePath,'filePathfilePath');
					let filerPath = await this.uploadFiles(filePath)
					imagePath.push(filerPath)
				}
				if(imagePath.length<1 || !this.content) {
					return uni.showToast({
						title:'请输入反馈内容',
						icon:'none'
					})
				}
				await this.update_feedback(imagePath)
				uni.hideLoading()
			},
			async uploadFiles(path){
				const result = await uniCloud.uploadFile({
					filePath:path,
					cloudPath:path
				})
				console.log(result);
				return result.fileID
			},
			async update_feedback(imagePath){
				let feedbacks = await this.$api.update_feedback({
					user_id: this.user_id,
					feedbackImages: imagePath,
					content: this.content,
				})
				if (!feedbacks) {
					uni.showToast({
						title:'反馈失败'
					})
				}else{
					uni.showToast({
						title:'反馈成功'
					})
					this.timeID = setTimeout(()=>{
						uni.navigateBack({delta: 1})
					},1030)
				}
				
			},
			addImage(){
				let count = 6 - this.imageList.length
				uni.chooseImage({
					count:count,
					success:(res) => {
						console.log(res);
						const tempFilePaths = res.tempFilePaths
						tempFilePaths.forEach((item, i)=>{
							// 处理H5多选的问题i
							if(i<count){
								this.imageList.push({
									url:item
								})
							}
						})
					}
				})
			},
		},
		beforeDestroy() {
			clearTimeout(this.timeID) 
		}
	}
</script>

<style scoped lang="scss">
.feedback-title{
	margin: 15px;
	margin-bottom: 0;
	font-size: 14px;
	color: #666;
}
.feedback-content{
	margin: 15px;
	padding: 10px;
	box-sizing: border-box;
	border: 1px #ddd solid;
	.feedback-text{
		font-size: 12px;
		width: 100%;
		height: 100px;
	}
}
.feedback-image-box{
	display: flex;
	flex-wrap: wrap;
	padding: 10px;
	.info-stre-image-item{
		position: relative;
		width: 33.33%;
		height: 0;
		padding-top: 33.33%;
		box-sizing: border-box;
		.close-icon{
			display: flex;
			justify-content: center;
			align-items: center;
			position: absolute;
			right: 0;
			top: 0;
			background-color: #ff5a5f;
			width: 22px;
			height: 22px;
			border-radius: 50%;
			z-index: 2;
		}
		.image-box{
			display: flex;
			justify-content: center;
			align-items: center;
			position: absolute;
			top: 5px;
			right: 5px;
			bottom: 5px;
			left: 5px;
			border: 1px solid #ddd;
			border-radius: 5px;
			overflow: hidden;
			image{
				width: 100%;
				height: 100%;
			}
		}
	}
}
.feedback-btn{
	margin: 0 15px;
	background-color: $my-bg-color;
}
</style>
